<!DOCTYPE html>
<html lang="zh">

<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta http-equiv="X-UA-Compatible" content="ie=edge">
	<title>FileReader</title>
</head>

<body>
	<input id="input" type="file">
	<script>
		// 获取元素
		const input = document.querySelector('input[type=file]')
		// 监听事件
		input.addEventListener('change', () => {
			// 创建FileReader实例对象
			const reader = new FileReader()
			// 调用readAsText()方法
			// reader.readAsText(input.files[0],'utf8') // input.files[0]为第一个文件
			// reader.readAsBinaryString(input.files[0],'utf8')
			reader.readAsArrayBuffer(input.files[0], 'utf8')
			// onload：当读取操作成功完成时调用
			reader.onload = () => {
				var data = buf2hex(reader.result);
				console.log(data, reader.result);
				document.body.innerHTML += reader.result;
				// document.body.innerHTML += Utf8ArrayToStr(reader.result)  // reader.result为获取结果
			}
		}, false)
		
		function getTime(text, reverse) {
			let timeReg = /\d{4}\-\d{2}\-\d{2} \d{2}:\d{2}:\d{2}/;
			for (let i = reverse ? text.length - 1 : 0; reverse ? i > -1 : i < text.length; reverse ? i-- : i++) {
				if (text[i].charCodeAt() === 10) {
					let snippet = text.substr(i + 1, 19);
					if (timeReg.exec(snippet)) {
						return snippet;
					}
				}
			}
		}

		//转码	
		function buf2hex(buffer) { // buffer is an ArrayBuffer
			return Array.prototype.map.call(new Uint8Array(buffer), x => ('00' + x.toString(16)).slice(-2)).join('');
		}

		//解码
		function hexToBuf(hex) {
			var typedArray = new Uint8Array(hex.match(/[\da-f]{2}/gi).map(function (h) {
				return parseInt(h, 16)
			}));
			return typedArray.buffer;
		}

		//字符串转ArrayBuff
		function strToArrayBuffer(s) {
			var i = s.length;
			var n = 0;
			var ba = new Array()
			for (var j = 0; j < i;) {
				var c = s.codePointAt(j);
				if (c < 128) {
					ba[n++] = c;
					j++;
				}
				else if ((c > 127) && (c < 2048)) {
					ba[n++] = (c >> 6) | 192;
					ba[n++] = (c & 63) | 128;
					j++;
				}
				else if ((c > 2047) && (c < 65536)) {
					ba[n++] = (c >> 12) | 224;
					ba[n++] = ((c >> 6) & 63) | 128;
					ba[n++] = (c & 63) | 128;
					j++;
				}
				else {
					ba[n++] = (c >> 18) | 240;
					ba[n++] = ((c >> 12) & 63) | 128;
					ba[n++] = ((c >> 6) & 63) | 128;
					ba[n++] = (c & 63) | 128;
					j += 2;
				}
			}
			return new Uint8Array(ba);
		}
		function Utf8ArrayToStr(array) {
			var out, i, len, c;
			var char2, char3;

			out = "";
			len = array.length;
			i = 0;
			while (i < len) {
				c = array[i++];
				switch (c >> 4) {
					case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
						// 0xxxxxxx
						out += String.fromCharCode(c);
						break;
					case 12: case 13:
						// 110x xxxx   10xx xxxx
						char2 = array[i++];
						out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
						break;
					case 14:
						// 1110 xxxx  10xx xxxx  10xx xxxx
						char2 = array[i++];
						char3 = array[i++];
						out += String.fromCharCode(((c & 0x0F) << 12) |
							((char2 & 0x3F) << 6) |
							((char3 & 0x3F) << 0));
						break;
				}
			}

			return out;
		}

	</script>
</body>

</html>